class WxRequest {
  //定义实例属性,用来设置默认请求参数
  defaults = {
    baseURL: "",
    url: "",
    data: null,
    method: "GET",
    header: {
      "Content-type": "application/json"
    },
    timeout: 60000,
    isLoading: true
  }
  //定义拦截器对象
  //需要包含请求拦截器，响应拦截器
  interceptors = {
    //请求拦截器
    request: (config) => config,
    //响应拦截器
    response: (response) => response
  }

  //定义数组队列
  queue = []

  constructor(params = {}) {
    this.defaults = Object.assign({}, this.defaults, params)
  }
  request(options) {
    //如果有新的请求，就清除上次的定时器
    this.timerId && clearTimeout(this.timerId)
    options.url = this.defaults.baseURL + options.url
    //合并请求参数
    options = {
      ...this.defaults,
      ...options
    }

    if (options.isLoading && options.method !== "UPLOAD") {
      this.queue.length === 0 && wx.showLoading({})
      this.queue.push("request")
    }
    //console.log(options)
    options = this.interceptors.request(options)
    return new Promise((resolve, reject) => {
      if (options.method === "UPLOAD") {
        wx.uploadFile({
          ...options,
          success: (res) => {
            //需要将服务器返回的json字符串转换成对象
            res.data = JSON.parse(res.data)
            const mergeRes = Object.assign({}, res, {
              config: options,
              isSuccess: true
            })
            resolve(this.interceptors.response(mergeRes))
          },
          fail: (err) => {
            res.data = JSON.parse(res.data)
            const mergeErr = Object.assign({}, err, {
              config: options,
              isSuccess: false
            })
            reject(this.interceptors.response(mergeErr))
          }
        })
      } else {
        wx.request({
          ...options,
          //接口调用成功时触发
          success: (res) => {
            const mergeRes = Object.assign({}, res, {
              config: options,
              isSuccess: true
            })
            resolve(this.interceptors.response(mergeRes))
          },
          //接口调用失败时触发
          fail: (err) => {
            const mergeErr = Object.assign({}, err, {
              config: options,
              isSuccess: false
            })
            reject(this.interceptors.response(mergeErr))
          },
          //接口调用结束的回调函数(调用成功、失败都会执行)
          complete: () => {
            if (options.isLoading) {
              this.queue.pop()
              this.queue.length === 0 && this.queue.push("request")
              this.timerId = setTimeout(() => {
                this.queue.pop()
                this.queue.length === 0 && wx.hideLoading()
                clearTimeout(this.timerId)
              }, 1)
            }
          }
        })
      }
    })
  }
  //封装get实例方法
  get(url, data = {}, config = {}) {
    return this.request(Object.assign({
      url,
      data,
      method: "GET"
    }, config))
  }

  //封装delete实例方法
  delete(url, data = {}, config = {}) {
    return this.request(Object.assign({
      url,
      data,
      method: "DELETE"
    }, config))
  }

  //封装post实例方法
  post(url, data = {}, config = {}) {
    return this.request(Object.assign({
      url,
      data,
      method: "POST"
    }, config))
  }

  //封装put实例方法
  put(url, data = {}, config = {}) {
    return this.request(Object.assign({
      url,
      data,
      method: "PUT"
    }, config))
  }

  all(...promise) {
    //console.log(promise)
    return Promise.all(promise)
  }

  //upload实例方法，用来对wx.uploadFile进行封装
  upload(url, filePath, name = "file", config = {}) {
    return this.request(Object.assign({
      url,
      filePath,
      name,
      method: "UPLOAD"
    }, config))
  }


}


//暴露
export default WxRequest